using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.EventSystems;

public class FrontClick : MonoBehaviour, IPointerClickHandler
{
    public float moveDuration = 0.5f;
    //front点击
    public void OnPointerClick(PointerEventData eventData)
    {
        // 获取点击的 GoodsC
        GameObject GoodsC = eventData.pointerCurrentRaycast.gameObject;
        // 获取选中的 Goods
        Goods selectedGoods = GoodsManager.Instance.GetSelectedGoods();

        // 确保有选中的 Goods
        if (selectedGoods != null)
        {
            selectedGoods.Deselect();
            // 获取目标位置
            Vector3 targetPosition = GoodsC.transform.position;

            selectedGoods.transform.SetParent(Data.levelsTransform);
            // 2. 使用 DOTween 创建平滑移动效果
            selectedGoods.transform.DOMove(targetPosition, 0.2f) // 0.5f 为移动时间
                .OnComplete(() =>
                {
                    AudioManager.Instance.PlaySoundEffect("sounds_place");
                    // 3. 动画完成后，恢复到 GoodsC 的父物体
                    selectedGoods.transform.SetParent(GoodsC.transform);

                    selectedGoods.container.BackToFront();
                    // 获取新的 Container
                    Container newContainer = GoodsC.transform.parent.parent.GetComponent<Container>();
                    selectedGoods.container = newContainer;
                    // 消除前面的 front
                    selectedGoods.container.ClearFront();
                });
        }
        else
        {
            Debug.Log("No Goods selected to move");
        }
    }

    public void MoniClick(GameObject goodsc)
    {
        Debug.Log("模拟点击");

        // 获取点击的 GoodsC
        GameObject GoodsC = goodsc;
        // 获取选中的 Goods
        Goods selectedGoods = GoodsManager.Instance.GetSelectedGoods();

        // 确保有选中的 Goods
        if (selectedGoods != null)
        {
            selectedGoods.Deselect();
            // 获取目标位置
            Vector3 targetPosition = GoodsC.transform.position;

            selectedGoods.transform.SetParent(Data.levelsTransform);
            // 2. 使用 DOTween 创建平滑移动效果
            selectedGoods.transform.DOMove(targetPosition, 0.2f) // 0.5f 为移动时间
                .OnComplete(() =>
                {
                    // 3. 动画完成后，恢复到 GoodsC 的父物体
                    selectedGoods.transform.SetParent(GoodsC.transform);

                    selectedGoods.container.BackToFront();
                    // 获取新的 Container
                    Container newContainer = GoodsC.transform.parent.parent.GetComponent<Container>();
                    selectedGoods.container = newContainer;
                    // 消除前面的 front
                    selectedGoods.container.ClearFront();
                });
        }
        else
        {
            Debug.Log("No Goods selected to move");
        }
    }
}
